/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.hslu.prg2.team2.logic;

import ch.hslu.prg2.team2.entity.Disc;
import ch.hslu.prg2.team2.entity.Player;
import java.io.Serializable;

/**
 *
 * @author Claudio
 */
public class GameboardAnalyzer implements Serializable
{
    private final Gameboard gameboard;

    public GameboardAnalyzer(Gameboard gameboard)
    {
        this.gameboard = gameboard;
    }

    public boolean hasPlayerWon(Player player)
    {
        return checkHorizontal(player) || 
                checkVertical(player) ||
                checkDiagonalTopDown(player) ||
                checkDiagonalBottomUp(player);
    }
    
    private boolean checkDiagonalBottomUp(Player player )
    {
        int consecutiveDiscs = 0;
        int horizontalOffset = 0;
        int verticalOffset = 3;
        int x = verticalOffset;
        int y = 0;
        while (x <= gameboard.getRowCount())
        {
            Disc disc = gameboard.getDisc(x,y);
            
            if( disc != null )
            {
                if( disc.getPlayer() == player )
                {
                    consecutiveDiscs++;
                }
                else
                {
                    consecutiveDiscs = 0;
                }
            }
            else
            {
                consecutiveDiscs = 0;
            }
            if (consecutiveDiscs == 4)
            {
                return true;
            }
            
            x--;
            y++;
            
            if( y > gameboard.getColumnCount()-1 )
            {
                x = -1;
            }
            
            if( x < 0 )
            {
                verticalOffset++;
                x = verticalOffset;
                y = 0;
                consecutiveDiscs = 0;
            }
            
            if( x > gameboard.getRowCount()-1 )
            {
                horizontalOffset++;
                y = horizontalOffset;
                x = gameboard.getRowCount() - 1;
                consecutiveDiscs = 0;
            }
            
            if( x == gameboard.getRowCount() -1 && y == gameboard.getColumnCount() -3 )
            {
                return false;
            }
        }
        return false;
    }

    private boolean checkDiagonalTopDown(Player player)
    {
        int consecutiveDiscs = 0;
        int horizontalOffset = 4;
        int verticalOffset = 0;
        int x = gameboard.getRowCount() - horizontalOffset;
        int y = 0;
        while (x <= gameboard.getRowCount())
        {
            Disc disc = gameboard.getDisc(x,y);
            if( disc != null )
            {
                if( disc.getPlayer() == player )
                {
                    consecutiveDiscs++;
                }
                else
                {
                    consecutiveDiscs = 0;
                }
            }
            else
            {
                consecutiveDiscs = 0;
            }
            if (consecutiveDiscs == 4)
            {
                return true;
            }
            if( x == gameboard.getRowCount()-1)
            {
                horizontalOffset++;
                x = gameboard.getRowCount() - horizontalOffset;
                y = 0;
                consecutiveDiscs = 0;
                
                if( x < 0 )
                {
                    x = 0;
                    verticalOffset++;
                    y = verticalOffset;
                    if( y == gameboard.getColumnCount() -3 )
                    {
                        return false;
                    }
                }
                consecutiveDiscs = 0;
                continue;
            }
            x++;
            y++;
            if( y == gameboard.getColumnCount() )
            {            
                x = 0;
                verticalOffset++;
                y = verticalOffset;
                consecutiveDiscs = 0;
                
                if( y == gameboard.getColumnCount() -3 )
                {
                    return false;
                }
            }
        }
        return false;
    }

    private boolean checkVertical(Player player)
    {
        int consecutiveDiscs = 0;
        for (int y = 0; y < gameboard.getColumnCount(); y++)
        {
            for (int x = 0; x < gameboard.getRowCount(); x++)
            {
                Disc disc = gameboard.getDisc(x,y);
                if( disc != null )
                {
                    if( disc.getPlayer() == player )
                    {
                        consecutiveDiscs++;
                    }
                    else
                    {
                        consecutiveDiscs = 0;
                    }
                }
                else
                {
                    consecutiveDiscs = 0;
                }
                if (consecutiveDiscs == 4)
                {
                    return true;
                }
            }
            consecutiveDiscs = 0;
        }
        return false;
    }

    private boolean checkHorizontal(Player player)
    {
        int consecutiveDiscs = 0;
        for (int x = 0; x < gameboard.getRowCount(); x++)
        {
            for (int y = 0; y < gameboard.getColumnCount(); y++)
            {
                Disc disc = gameboard.getDisc(x, y);
                if (disc != null)
                {
                    if (disc.getPlayer() == player)
                    {
                        consecutiveDiscs++;
                    }
                    else
                    {
                        consecutiveDiscs = 0;
                    }
                }
                else
                {
                    consecutiveDiscs = 0;
                }
                if (consecutiveDiscs == 4)
                {
                    return true;
                }
            }
            consecutiveDiscs = 0;
        }
        return false;
    }

    public boolean isGameFinished()
    {
        for (int i = 0; i < gameboard.getColumnCount(); i++)
        {
            if (gameboard.getDisc(0, i) == null)
            {
                return false;
            }
        }

        return true;
    }
}
